package com.lzy.concertticket.service.impl;

import com.alibaba.excel.EasyExcel;
import com.lzy.concertticket.dao.FlowDao;
import com.lzy.concertticket.model.entity.FlowEntity;
import com.lzy.concertticket.model.req.FlowReq;
import com.lzy.concertticket.model.vo.FlowVo;
import com.lzy.concertticket.service.FlowService;
import com.lzy.concertticket.threadlocal.ResponseThreadLocal;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlowServiceImpl implements FlowService {

    @Autowired
    private FlowDao flowDao;


    @Override
    public void saveBatch(List<FlowEntity> flowEntityList) {
        flowDao.saveBatch(flowEntityList);
    }

    @Override
    public List<FlowVo> count(FlowReq flowReq) {

        List<FlowVo> flowVoList = flowDao.countFlow(flowReq);
        return flowVoList;
    }

    @SneakyThrows
    @Override
    public void export() {

        List<FlowEntity> flowList = flowDao.list();
        HttpServletResponse response = ResponseThreadLocal.get();
        response.setContentType("application/vnd.ms-excel");
        ServletOutputStream outputStream = response.getOutputStream();
        EasyExcel.write(outputStream, FlowEntity.class).sheet().doWrite(flowList);

    }
}
